package com.cdut.drivingschool.ui.main;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.cdut.drivingschool.R;
/**
 * 主页面
 * @author ZhaoHB
 *
 */
public class MainActivity extends Activity {
	private RadioGroup mTabGroup;
	/**
	 * 首页
	 */
	private HomeFragment mHomeFrag;
	/**
	 * 题库
	 */
	private LibFragment mLibFrag;
	/**
	 * 我的
	 */
	private MyFragment mMyFrag;
	/**
	 * 更多
	 */
	private MoreFragment mMoreFrag;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		((RadioButton)this.findViewById(R.id.rb_tab_home)).setChecked(true);;
		mTabGroup = (RadioGroup) findViewById(R.id.group_tab);
		mTabGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.rb_tab_home:
					switchContent(1);
					break;
				case R.id.rb_tab_lib:
					switchContent(2);
					break;
				case R.id.rb_tab_my:
					switchContent(3);
					break;
				case R.id.rb_tab_more:
					switchContent(4);
					break;
				default:
					break;
				}
			}
		});
		/**初始化界面*/
		FragmentManager fragmentManager = getFragmentManager();
		FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
		mHomeFrag = HomeFragment.newInstance();
		mLibFrag = LibFragment.newInstance();
		mMyFrag = MyFragment.newInstance();
		mMoreFrag = MoreFragment.newInstance();
		fragmentTransaction.add(R.id.frag_container, mHomeFrag);
		fragmentTransaction.add(R.id.frag_container, mLibFrag);
		fragmentTransaction.hide(mLibFrag);
		fragmentTransaction.add(R.id.frag_container, mMyFrag);
		fragmentTransaction.hide(mMyFrag);
		fragmentTransaction.add(R.id.frag_container, mMoreFrag);
		fragmentTransaction.hide(mMoreFrag);
		fragmentTransaction.commit();
	}
	/**
	* 跳转页面
	* 
	* @param index
	*/
	public void switchContent(int index) {
		FragmentTransaction transaction = getFragmentManager().beginTransaction();
		transaction.hide(mHomeFrag);
		transaction.hide(mLibFrag);
		transaction.hide(mMyFrag);
		transaction.hide(mMoreFrag);
		switch (index) {
		case 1:
			transaction.show(mHomeFrag);
			break;
		case 2:
			transaction.show(mLibFrag);
			break;
		case 3:
			transaction.show(mMyFrag);
			break;
		case 4:
			transaction.show(mMoreFrag);
			break;
		default:
			break;
		}
		transaction.commit();
	}
}
